$(function() {
    let id;

    function getAll() {
        $.ajax({
            type: 'get',
            url: BigNew.category_list,
            success: function(res) {
                console.log(res);
                if (res.code == 200) {
                    let htmlStr = template('tp-list', res)
                    $('tbody').html(htmlStr)
                }
            }
        })

    }
    getAll();

    //区分模态框显示按钮的来源
    $('#myModal').on('show.bs.modal', function(e) {
        // console.log(e.relatedTarget)
        if (e.relatedTarget.innerText == '新增分类') {
            $('.modal-title').text('新增分类');
            $('.btn-edit').text('新增');
        } else {
            $('.modal-title').text('编辑分类');
            $('.btn-edit').text('编辑');

            id = e.relatedTarget.getAttribute('data-id');
            //发起请求
            $.ajax({
                type: 'get',
                url: BigNew.category_search,
                data: {
                    id: id
                },
                success: function(res) {
                    console.log(res);
                    $('#recipient-name').val(res.data[0].name);
                    $('#message-text').val(res.data[0].slug);
                }
            })
        }
    })

    $('.btn-edit').on('click', function() {
        if ($(this).text() == '新增') {
            let name = $('#recipient-name').val().trim();
            console.log(name);
            let slug = $('#message-text').val().trim();
            console.log(slug);
            if (name == '' || slug == '') {
                return;
            }
            $.ajax({
                type: 'post',
                url: BigNew.category_add,

                data: {
                    name: name,
                    slug: slug
                },
                success: function(res) {
                    console.log(res);
                    alert(res.msg)
                    $('#myModal').modal('hide')
                    getAll()
                }
            })
        } else {
            let name = $('#recipient-name').val().trim();
            console.log(name);
            let slug = $('#message-text').val().trim();
            console.log(slug);
            console.log(id);
            $.ajax({
                type: 'post',
                url: BigNew.category_edit,
                data: {
                    id: id,
                    name: name,
                    slug: slug
                },
                success: function(res) {
                    console.log(res);
                    if (res.code == 200) {
                        alert(res.msg);
                        $('#myModal').modal('hide')
                        getAll();
                    }
                }
            })
        }
    })


    //删除功能
    $('tbody').on('click', '.btn-del', function() {
        // console.log($(this))
        let id = $(this).siblings('a').attr('data-id')
        console.log(id);
        $.ajax({
            type: 'post',
            url: BigNew.category_delete,
            data: {
                id: id
            },
            success: function(res) {
                console.log(res);
                if (res.code == 204) {
                    alert(res.msg)
                    getAll()
                }


            }
        })
    })

    //隐藏模态框
    $('#myModal').on('hidden.bs.modal', function() {
        $('#recipient-name').val('');
        $('#message-text').val('');

    })

})